package cn.itcast.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * @param
 * @return
 */
@WebListener//监听器的注解
public class ServletContextListenerDemo implements ServletContextListener {
    //这个是测试监听ServletContext

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //监听ServletContext对象的创建
        System.out.println("ServletContext对象被创建了");
        //获取ServletContext，看看是不是创建出来的那个
        ServletContext servletContext = sce.getServletContext();
        System.out.println("servletContext = " + servletContext);
        //添加属性,以此触发添加属性的监听器
        servletContext.setAttribute("username","秃三炮");
        //替换属性,以此触发替换属性的监听器
        servletContext.setAttribute("username","李四");
        //删除属性,以此触发删除属性的监听器
        servletContext.removeAttribute("username");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //监听ServletContext对象的销毁
        System.out.println("ServletContext对象被销毁了");
    }
}
